//
//  MineViewController.swift
//  newSwift
//
//  Created by MAC on 2023/10/18.
//

import UIKit


class MineViewController : BaseViewController{
    
    let dataSource : [[[String: String]]] = [
        [
            ["title":"健康记录","icon":"mine_health"],
            ["title":"运动记录","icon":"mine_sport"],
            ["title":"潜水记录","icon":"mine_diving"],
            ["title":"运动目标","icon":"mine_target"],
        ],
        [
            ["title":"帮助与反馈","icon":"mine_health"],
            ["title":"关于我们","icon":"mine_about"],
        ]
    ]
    
    lazy var bgImv : UIImageView = {
        let imv = UIImageView()
        imv.image = R.image.mine_bg()
        return imv
    }()
    
    ///tableview
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(MineCell.self, forCellReuseIdentifier: MineCell.reuseId)
        tableView.backgroundColor = UIColor.clear
        tableView.tableFooterView = UIView()
        return tableView
    }()
    
    /// viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "我的"
        
        let button = UIButton()
        button.frame = CGRectMake(0, 0, 36, 40)
        button.setTitle("登出", for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 16)
        button.setTitleColor(UIColor.init(hex: 0x333333)!, for: .normal)
        button.addTarget(self, action: #selector(logout), for: .touchUpInside)
        
        let rightBarItem : UIBarButtonItem = UIBarButtonItem(customView: button)
        
        self.navigationItem.rightBarButtonItem = rightBarItem
        
        let string = "111"
        let num : Int?
        
        num = string.int
        
        print("\(num!.string)")
        
        setupViews()
        let s = self.test().string
        
        print(s)
    }
    
    func test()->Int{
        7
    }
    
    
    func setupViews(){
        
        view.addSubview(bgImv)
        bgImv.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    @objc func logout(){
//        UserDefaults().setValue(false
//                                , forKey: "isLogin")
//        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SwicthRootViewControllerNotification"), object: nil)
        
    }
}


//MARK: tableView 代理
extension MineViewController : UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: MineCell.reuseId, for: indexPath) as! MineCell
        
        let arr = dataSource[indexPath.section]
        let dic = arr[indexPath.row]
        
        cell.titleLabel.text = dic["title"]
        cell.iconImv.image = UIImage(named: dic["icon"]!)
        
        if (indexPath.row == (dataSource[indexPath.section].count - 1)) || indexPath.row == 0 {
            
        }else{
            
        }
        
        return cell
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 4
        }
        if section == 1 {
            return 2
        }
        
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        50
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        20
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 20))
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRectMake(0, 0, screenWidth, 40))
        let label = UILabel(frame:CGRectMake(20, 0, 100, 40))
        label.font = .systemFont(ofSize: 14 , weight: .medium)
        label.textColor = UIColor.init(hex: 0x333333)!
        if section == 0 {
            label.text = "健康数据"
        }else{
            label.text = "其他"
        }
        view .addSubview(label)
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
        // 绘制Section边框
        tableView.setCornerRadiusSection(willDisplay: cell, forRowAt: indexPath)
       }

    
}


extension UITableView {
    /// section圆形边框
    func setCornerRadiusSection(radius: CGFloat = 10.0, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 圆角半径
        let cornerRadius = radius
        // 下面为设置圆角操作（通过遮罩实现）
        let sectionCount = self.numberOfRows(inSection: indexPath.section)
        cell.layer.mask = nil
        // 当前分区有多行数据时
        if sectionCount > 1 {
            switch indexPath.row {
            // 如果是第一行,左上、右上角为圆角
            case 0:
                cell.bbCornerCorner(corner: [.topLeft,.topRight], radii: cornerRadius)
            // 如果是最后一行,左下、右下角为圆角
            case sectionCount - 1:
                cell.bbCornerCorner(corner: [.bottomLeft,.bottomRight], radii: cornerRadius)
            default: break
            }
        }
        //当前分区只有一行行数据时
        else {
            //四个角都为圆角（同样设置偏移隐藏首、尾分隔线）
            cell.bbCornerCorner(corner: [.allCorners], radii: cornerRadius)
        }
    }
}
extension UIView {
    /// 自定义控件圆角位置 如：只左上 左下有圆角
    func bbCornerCorner(corner: UIRectCorner, radii: CGFloat){
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corner, cornerRadii: CGSize(width: radii, height: radii))
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }
}
